﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ParkingLot.Utility.StockHelp
{
    /*本机一个端口,发送方一个端口*/

    public class Send
    {
        private UdpClient m_NewSock;

        /// <summary>
        /// 默认广播地址
        /// </summary>
        private IPAddress m_Multicast;

        /// <summary>
        /// 默认广播端口
        /// </summary>
        private int m_MulticastPort;

        #region Instance

        private static Send m_Instance;

        public static Send Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new Send();
                return m_Instance;
            }
        }

        #endregion

        /// <summary>
        /// 发送数据包到某个IP地址的某个端口
        /// </summary>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="port">端口</param>
        /// <param name="data">数据</param>
        public void StartSend(string ipAddress, int port, byte[] data)
        {
            //写日志记录数据发送情况
            StringBuilder sbError = new StringBuilder();
            //设置发送端口
            Random r = new Random();
            bool isSuccessSetPort = false;
            while (!isSuccessSetPort)
            {
                //源端口  目的端口
                int localPort = r.Next() % 1000 + 1000;
                try
                {
                    m_NewSock = new UdpClient(localPort);
                    isSuccessSetPort = true;
                }
                catch (Exception e)
                {
                    localPort = r.Next() % 1000 + 1000;
                }
            }
            //是否发送或接受广播
            m_NewSock.EnableBroadcast = true;
            //自发自收
            m_NewSock.MulticastLoopback = true;
            try
            {
                m_NewSock.Send(data, data.Length, new IPEndPoint(IPAddress.Parse(ipAddress), port));

            }
            catch (ArgumentNullException ex)
            {
                sbError.AppendLine(DateTime.Now.ToString() + "dgram 为 null" + ex.Message);
            }
            catch (ObjectDisposedException ex)
            {
                sbError.AppendLine(DateTime.Now.ToString() + "System.Net.Sockets.UdpClient 被关闭" + ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                sbError.AppendLine(DateTime.Now.ToString() + " System.Net.Sockets.UdpClient 已建立默认远程主机。" + ex.Message);
            }
            catch (SocketException ex)
            {
                sbError.AppendLine(DateTime.Now.ToString() + "访问套接字时出错。有关更多信息，请参见备注部分" + ex.Message);
            }
            catch (Exception ex)
            {
                sbError.AppendLine(DateTime.Now.ToString() + ex.Message);
            }
            finally
            {
                m_NewSock.Close();
            }

        }

      

        /// <summary>
        /// 设置广播地址
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public bool SetMulticast(string ip, int port)
        {
            try
            {
                m_Multicast = IPAddress.Parse(ip);
                m_MulticastPort = port;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
